﻿using System;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading.Tasks;
using TK.TSocket;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpSocketServer server = new TcpSocketServer((config) =>
            {
                //config.DataAdapterFunc = (adapterConfig) =>
                //{
                //    return new TouchSocket.Sockets.FixedHeaderPackageAdapter();
                //};
            });
            ReceiveData receive = new ReceiveData(server);
            server.Start(receive);
            Console.WriteLine($"socket服务监听中......");
            Console.ReadKey();
            server.Dispose();
        }
    }

    public class ReceiveData : IReceiveData
    {
        private readonly ITcpSocketServer _server;

        public ReceiveData(ITcpSocketServer server)
        {
            _server = server;
        }

        public Task ReceiveDataAsync(DataPacket dataPacket)
        {
            DataPacket tt = dataPacket;
            Console.WriteLine(JsonSerializer.Serialize(dataPacket, new JsonSerializerOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }));
            if (dataPacket.DataType == DataType.Receive)
            {
                _server.SendData(dataPacket.ClientId, dataPacket.Data);
            }
            return Task.CompletedTask;
        }
    }
}
